﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Cryptography;

namespace Nasty
{
    public class Encryption
    {
        #region encryptionTest1
        private static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("");

        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                throw new ArgumentNullException("The string which needs to be encrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }

        public static string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException("The string which needs to be decrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream
                    (Convert.FromBase64String(cryptedString));
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);
            return reader.ReadToEnd();
        }
        #endregion


        //Doit être de 64 bits, 8 octets.
        

        //static void Main(string[] args)
        //{
        //    EncryptFile("c:\\temp\\test.txt",
        //      "c:\\temp\\Encrypted.txt",
        //      sSecretKey);

        //    DecryptFile("c:\\temp\\Encrypted.txt",
        //      "c:\\temp\\Decrypted.txt",
        //      sSecretKey);
        //}
        private const string sSecretKey = "Password";
        public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
        {
            FileStream fsInput = new FileStream(sInputFilename,
                                                FileMode.Open,
                                                FileAccess.Read);

            FileStream fsEncrypted = new FileStream(sOutputFilename,
                                                    FileMode.Create,
                                                    FileAccess.Write);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsEncrypted,
                                                         desencrypt,
                                                         CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();
        }
        public static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
        {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            //Une clé 64 bits et IV est nécessaire pour ce fournisseur.
            //Définit la clé secrète pour l'algorithme DES.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            //Définit le vecteur d'initialisation.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            //Crée un flux de fichier pour lire le fichier crypté.
            FileStream fsread = new FileStream(sInputFilename,
                                               FileMode.Open,
                                               FileAccess.Read);
            //Crée un décrypteur DES à partir de l'instance DES.
            ICryptoTransform desdecrypt = DES.CreateDecryptor();
            //Crée un flux crypto défini pour lire et effectue une
            //transformation de cryptage DES sur les octets entrants.
            CryptoStream cryptostreamDecr = new CryptoStream(fsread,
                                                             desdecrypt,
                                                          CryptoStreamMode.Read);
            //Imprime le contenu du fichier décrypté.
            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
            fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        } 
    }
}
